﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Media;
using System.IO;
using Microsoft.Win32;

using 地圖檔案序列化;

using Microsoft.Kinect;
using Microsoft.Kinect.Toolkit;
using Microsoft.Kinect.Toolkit.Controls;



namespace FizzGame
{
    /// <summary>
    /// GamePage.xaml 的互動邏輯
    /// </summary>
    public partial class GamePage : Page
    {
        System.Windows.Threading.DispatcherTimer dispatcherTimer;

        string filename; 

        Object PlayerRect = new Object();
        Rectangle Player = new Rectangle();

        List<Enemy> enemy = new List<Enemy>();
        List<Rectangle> enemyRect = new List<Rectangle>();

        List<Object> ObjectMap = new List<Object>();
        List<Object> GroundMap = new List<Object>();
        List<Object.Postion> EnemyCreate = new List<Object.Postion>();

        Object NewEnemy = new Object();
        Rectangle NewEnemyRect = new Rectangle();
          
        Image BackGround = new Image();

        prtsc pc = new prtsc();

        public GamePage(string param,Uri uri)
        {
            InitializeComponent();

            filename = param;

            BackGround.Source = new BitmapImage(uri);
            BackGround.Height = 1680;
            BackGround.Width = 3840;
            GameCanvas.Children.Add(BackGround);
            Canvas.SetLeft(BackGround, 0);
            Canvas.SetTop(BackGround, 0);

            DrawMap();

            Player.Width = 175;
            Player.Height = 190;
            Player.Fill = new SolidColorBrush(Colors.Black);
            GameCanvas.Children.Add(Player);
            Canvas.SetLeft(Player, 100);
            Canvas.SetTop(Player, 110);

            PlayerRect.size.Height = 175;
            PlayerRect.size.Width = 80;
            PlayerRect.Pos.X = 110;
            PlayerRect.Pos.Y = 170;
            PlayerRect.HP = 385;


            NewEnemyRect.Width = 175;
            NewEnemyRect.Height = 190;
            NewEnemyRect.Fill = new SolidColorBrush(Colors.Yellow);
            GameCanvas.Children.Add(NewEnemyRect);
            Canvas.SetLeft(NewEnemyRect, 2090);
            Canvas.SetTop(NewEnemyRect, 100);

            NewEnemy.size.Height = 175;
            NewEnemy.size.Width = 80;
            NewEnemy.Pos.X = 2100;
            NewEnemy.Pos.Y = 170;
            NewEnemy.IsBack = true;
            NewEnemy.HP = 100;

            dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
            dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
            dispatcherTimer.Interval = TimeSpan.FromMilliseconds(30);
            dispatcherTimer.Start();

        }

        Vector PlayerV = new Vector(0, 0);
        Vector EnemyV = new Vector(0, 0);
        void dispatcherTimer_Tick(object sender, EventArgs e)
        {
            PlayerMoveControl();
            EnemyMoveControl(NewEnemy, NewEnemyRect);
          //  EnemyMoveControl(PlayerRect, Player);
        }
        void DrawMap()
        {
            Map MapBlock = new Map((560 * 1280) / (20 * 20));


             Stream stream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read);
             MapBlock.DeSerializeNow(stream);
             stream.Close();

            for (int j = 0; j < MapBlock.MapDataSize; j++)
            {
                Color cc;
                switch (MapBlock.data[j].Value)
                {
                    case 0:
                        cc = Colors.Blue;
                        break;
                    case 1:
                        cc = Colors.Green;
                        break;
                    case 2:
                        cc = Colors.Yellow;
                        break;
                    default:
                        cc = Colors.Yellow;
                        break;
                }
                Rectangle Setrect = new Rectangle();
                Setrect.Fill = new SolidColorBrush(cc);

                Setrect.Width = MapBlock.data[j].Size * 3;
                Setrect.Height = MapBlock.data[j].Size * 3;
  //              GameCanvas.Children.Add(Setrect);
  //              Canvas.SetLeft(Setrect, MapBlock.data[j].Pos.X *3);
  //              Canvas.SetTop(Setrect, MapBlock.data[j].Pos.Y * 3);
  //              Canvas.SetZIndex(Setrect, 0);
                Object MBlock = new Object();
                MBlock.Pos.X = (int)MapBlock.data[j].Pos.X*3 ;
                MBlock.Pos.Y = (int)MapBlock.data[j].Pos.Y*3 ;

                MBlock.size.Height = MapBlock.data[j].Size*3 ;
                MBlock.size.Width = MapBlock.data[j].Size*3 ;

                if (MapBlock.data[j].Value == 1)
                    GroundMap.Add(MBlock);
                else if (MapBlock.data[j].Value == 0)
                {
                    ObjectMap.Add(MBlock);
                    GroundMap.Add(MBlock);
                }
                else if (MapBlock.data[j].Value == 2)
                {
                    Object.Postion newPos = new Object.Postion();
                    newPos.X = (int)MapBlock.data[j].Pos.X*3;
                    newPos.Y = (int)MapBlock.data[j].Pos.Y*3;
                    EnemyCreate.Add(newPos);
                }
            }
        }
        void SetCanvasPos(UIElement Target,int X, int Y)
        {
            Canvas.SetLeft(Target, X);
            Canvas.SetTop(Target, Y);
        }
        private void Window_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key.ToString() == "Right")
            {
                PlayerRect.IsRunning = true;
            }
            if (e.Key.ToString() == "Left")
            {
                PlayerRect.IsBack = true;
            }
            if (e.Key.ToString() == "S" && PlayerRect.IsGround)
            {
                PlayerRect.IsJump = true;
            }
            if (e.Key.ToString() == "D" )
            {
                PlayerRect.IsAttack = true;
            }
            //未處理~~~~~~~~~~~~~~~~
            if (e.Key.ToString() == "Down" && e.Key.ToString() == "S")
            {
                PlayerRect.IsDown = true;
            }
        }
        private void Window_KeyUp(object sender, KeyEventArgs e)
        {

            if (e.Key.ToString() == "Right")
            {
                PlayerRect.IsRunning = false;
            }
            if (e.Key.ToString() == "Left")
            {
                PlayerRect.IsBack = false;
            }

        }
        private void mediaElement1_MediaOpened()
        {

        }
        int framei=1;
        int frameAj = 1;
        bool A = true;
        int hcout=0;
        private void PlayerMoveControl()
        {
            Vector g = new Vector(0, 0.4);

            PlayerV += g;
            if (PlayerV.Y > 13)
                PlayerV.Y = 13;

           if (PlayerRect.IsJump)
            {
                PlayerV += new Vector(0, -13);
                PlayerRect.IsJump = false;
            }

            PlayerRect.IsGround = false;
            for (int i = 0; i < GroundMap.Count; i++)
            {
                PlayerRect.Pos.Y += (int)PlayerV.Y;
                if (PlayerRect.TouchDetect(GroundMap[i], 2))
                {
                    if (PlayerV.Y > 0)
                    {
                        PlayerV = new Vector(PlayerV.X, 0);
                        PlayerRect.IsGround = true;
                    }
                }
                PlayerRect.Pos.Y -= (int)PlayerV.Y;
            }

            if (PlayerRect.IsRunning)
            {
                A = true;
                if (PlayerV.X < 5)
                    PlayerV.X = 5;
                if (PlayerRect.IsGround)
                PlayerV.X += 1;
                if (PlayerV.X > 10)
                    PlayerV.X = 10;
                for (int i = 0; i < ObjectMap.Count; i++)
                {
                    if (PlayerRect.TouchDetect(ObjectMap[i], 3))
                    {
                        PlayerV = new Vector(0, PlayerV.Y);
                    }
                }
            }

            if (PlayerRect.IsBack)
            {
                A = false;
                if (PlayerV.X > -5)
                    PlayerV.X = -5;
                if (PlayerRect.IsGround)
                PlayerV.X -= 1;
                if (PlayerV.X < -10)
                    PlayerV.X = -10;
                for (int i = 0; i < ObjectMap.Count; i++)
                {
                    if (PlayerRect.TouchDetect(ObjectMap[i], 4))
                    {
                        PlayerV = new Vector(0, PlayerV.Y);
                    }
                }
            }

            if (PlayerRect.IsHurt)
            {
                if (NewEnemy.Pos.X > PlayerRect.Pos.X)
                    PlayerV.X = -5;
                else if (NewEnemy.Pos.X < PlayerRect.Pos.X)
                    PlayerV.X = +5;
                hcout++;
                PlayerRect.HP -= 2;
                if (hcout > 10)
                {
                    PlayerRect.IsHurt = false;
                    hcout = 0;
                }
            }

            if (!PlayerRect.IsBack && !PlayerRect.IsRunning)
            {
                PlayerV.X = 0;
            }
            


            if (PlayerRect.IsRunning && A)
            {

                ImageBrush PlayerImage = new ImageBrush();
                PlayerImage.ImageSource = new BitmapImage(new Uri(@"C:\Users\意喬\SkyDrive\Documents\程式專案\FizzGame\FizzGame\Image\Running\Running ("+framei.ToString()+").png"));
                Player.Fill = PlayerImage;
                if (PlayerRect.IsGround)
                framei++;
                if (framei > 40) framei = 1;
            }
            else if (PlayerRect.IsBack && A == false)
            {
                ImageBrush PlayerImage = new ImageBrush();
                PlayerImage.ImageSource = new BitmapImage(new Uri(@"C:\Users\意喬\SkyDrive\Documents\程式專案\FizzGame\FizzGame\Image\Back\Back (" + framei.ToString() + ").png"));
                Player.Fill = PlayerImage;
                if (PlayerRect.IsGround)
                framei++;
                if (framei > 40) framei = 1;
            }

            if (PlayerRect.IsAttack && A)
            {
                ImageBrush PlayerImage = new ImageBrush();
                PlayerImage.ImageSource = new BitmapImage(new Uri(@"C:\Users\意喬\SkyDrive\Documents\程式專案\FizzGame\FizzGame\Image\Attack\Attack (" + frameAj.ToString() + ").png"));
                Player.Fill = PlayerImage;
                frameAj++;
                if (frameAj > 25)
                {
                     frameAj = 1;
                     PlayerRect.IsAttack = false;
                }
            }

            if (PlayerRect.IsAttack && A == false)
            {
                ImageBrush PlayerImage = new ImageBrush();
                PlayerImage.ImageSource = new BitmapImage(new Uri(@"C:\Users\意喬\SkyDrive\Documents\程式專案\FizzGame\FizzGame\Image\AttackB\Attack (" + frameAj.ToString() + ").png"));
                Player.Fill = PlayerImage;
                frameAj++;
                if (frameAj > 25)
                {
                    frameAj = 1;
                    PlayerRect.IsAttack = false;
                }
            }


            PlayerRect.Pos.X += (int)PlayerV.X;
            PlayerRect.Pos.Y += (int)PlayerV.Y;

            //check
           Object.Postion RealPos = PlayerRect.Pos;
           int Y = (int )Canvas.GetTop(Player);
           int X = (int)Canvas.GetLeft(Player);
           Canvas.SetTop(BackGround, Y - PlayerRect.Pos.Y);
           Canvas.SetLeft(BackGround, X - PlayerRect.Pos.X+40);
           // PlayerV.X = 0;
            if(A)
                ATest.Fill = new SolidColorBrush(Colors.Red);
            else
                ATest.Fill = new SolidColorBrush(Colors.Green);



           if (PlayerRect.HP > 0)
               RHP.Width = PlayerRect.HP;
           else
           {
               GameCanvas.Children.Clear();
               BackGround.Source = pc.GetGrayPrtSc();
               BackGround.Height = 768;
               BackGround.Width = 1366;
               GameCanvas.Children.Add(BackGround);
               Canvas.SetTop(BackGround, 0);
               Canvas.SetLeft(BackGround, 0);
               GameOver();
               dispatcherTimer.Stop();
           }
        }
        private void EnemyMoveControl( Object Obj , Rectangle Rect)
        {
            Vector g = new Vector(0, 0.4);

            Object AttackArea = new Object();

            EnemyV += g;
            if (EnemyV.Y > 12)
                EnemyV.Y = 12;

            AttackArea.size.Width = 40;
            AttackArea.size.Height = 170;

            if (A)
            {
                AttackArea.Pos.X = PlayerRect.Pos.X + 80;
                AttackArea.Pos.Y = PlayerRect.Pos.Y;
            }
            else
            {
                AttackArea.Pos.X = PlayerRect.Pos.X - 40;
                AttackArea.Pos.Y = PlayerRect.Pos.Y;
            }

            if (Obj.TouchDetect(AttackArea))
            {
                if (PlayerRect.IsAttack)
                {
                    Obj.IsBack = false;
                    Obj.IsRunning = false;
                    if (A == false)
                        Obj.IsBack = true;
                    else
                        Obj.IsRunning = true;
                    Obj.IsHurt = true;
                }
            }
            if (Obj.TouchDetect(PlayerRect))
            {
                PlayerRect.IsHurt = true;
                if (Obj.IsBack)
                {
                    Obj.IsBack = false;
                    Obj.IsRunning = true;
                }
                else
                {
                    Obj.IsBack = true;
                    Obj.IsRunning = false;
                }
            }

            if (Obj.IsJump)
            {
                EnemyV += new Vector(0, -13);
                Obj.IsJump = false;
            }

            Obj.IsGround = false;
            for (int i = 0; i < GroundMap.Count; i++)
            {
                Obj.Pos.Y += (int)EnemyV.Y;
                if (Obj.TouchDetect(GroundMap[i], 2))
                {
                    if (EnemyV.Y > 0)
                    {
                        EnemyV = new Vector(EnemyV.X, 0);
                        Obj.IsGround = true;
                    }
                }
                Obj.Pos.Y -= (int)EnemyV.Y;
            }

            if (Obj.IsHurt)
            {
                Obj.HP -= 2;
                if (Obj.HP <= 0)
                {
                    GameCanvas.Children.Remove(Rect);
                    GameWin();
                    dispatcherTimer.Stop();
                }
                Obj.IsHurt = false;
            }

            if (Obj.IsRunning)
            {
                ImageBrush RectImage = new ImageBrush();
                RectImage.ImageSource = new BitmapImage(new Uri(@"C:\Users\意喬\Documents\Visual Studio 2012\Projects\FizzGame\FizzGame\Image\小兵B.png"));
                Rect.Fill = RectImage;
                // A = true;
                EnemyV.X = 2;
                for (int i = 0; i < ObjectMap.Count; i++)
                {
                    if (Obj.TouchDetect(ObjectMap[i], 3))
                    {
                        EnemyV = new Vector(0, EnemyV.Y);
                        Obj.IsBack = true;
                        Obj.IsRunning = false;
                    }
                }
            }

            if (Obj.IsBack)
            {
                ImageBrush RectImage = new ImageBrush();
                RectImage.ImageSource = new BitmapImage(new Uri(@"C:\Users\意喬\Documents\Visual Studio 2012\Projects\FizzGame\FizzGame\Image\小兵.png"));
                Rect.Fill = RectImage;
                //A = false;
                EnemyV.X = -2;
                for (int i = 0; i < ObjectMap.Count; i++)
                {
                    if (Obj.TouchDetect(ObjectMap[i], 4))
                    {
                        EnemyV = new Vector(0, EnemyV.Y);
                        Obj.IsRunning = true;
                        Obj.IsBack = false;
                    }
                }
            }

            if (!Obj.IsBack && !Obj.IsRunning)
            {
                EnemyV.X = 0;
            }

            Obj.Pos.X += (int)EnemyV.X;
            Obj.Pos.Y += (int)EnemyV.Y;

            int Y = (int)Canvas.GetTop(Player);
            int X = (int)Canvas.GetLeft(Player);
            Canvas.SetTop(Rect, Y - (PlayerRect.Pos.Y - Obj.Pos.Y));
            Canvas.SetLeft(Rect, X - (PlayerRect.Pos.X - Obj.Pos.X) + 40);
            Canvas.SetTop(enemyhp, Y - (PlayerRect.Pos.Y - Obj.Pos.Y + 20));
            Canvas.SetLeft(enemyhp, X - (PlayerRect.Pos.X - Obj.Pos.X) + 40);
            enemyhp.Width = Obj.HP;

            // Canvas.SetTop(Rect, Obj.Pos.Y);
            // Canvas.SetLeft(Rect,Obj.Pos.X);
        }
        private void GameOver()
        {
            SoundPlayer player = new SoundPlayer(@"C:\Users\意喬\Documents\Visual Studio 2012\Projects\FizzGame\FizzGame\Music\Defeat.wav");
            player.Play();
            Clean();
        }
        private void GameWin()
        {


            Rectangle Logo = new Rectangle();
            Logo.Width = 893;
            Logo.Height = 515;


            ImageBrush RectImage = new ImageBrush();
            RectImage.ImageSource = new BitmapImage(new Uri(@"C:\Users\意喬\Documents\Visual Studio 2012\Projects\FizzGame\FizzGame\Image\Victory.png"));
            Logo.Fill = RectImage;

            KinectRegion region = new KinectRegion();

            GameCanvas.Children.Add(Logo);

            Canvas.SetLeft(Logo, 237);
            Canvas.SetTop(Logo, 100);

            SoundPlayer player = new SoundPlayer(@"C:\Users\意喬\Documents\Visual Studio 2012\Projects\FizzGame\FizzGame\Music\Victory.wav");
            player.Play();

        }

        private void ButtonOnClick(object sender, RoutedEventArgs e)
        {
            MenuPage gamepage = new MenuPage();
            this.NavigationService.Navigate(gamepage);
        }


    }
}
